﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace CoreInmobiliario.Frontend
{
    public partial class Noticias : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
                GetNoticias();

            if (Session[HelperClasses.Parameters.USERID] == null || Session[HelperClasses.Parameters.USERNAME] == null)
            {
                btnNewNoticia.Visible = false;
            }
        }

        public void GetNoticias()
        {
            gvNoticia.DataSource = Business.Noticia.GetInstance().GetNoticias();
            gvNoticia.DataBind();

            foreach (GridViewRow row in gvNoticia.Rows)
            {
                //Setea imagen a Noticias
                Image imgNoticia = row.FindControl("imgNoticia") as Image;
                string noticiaId = gvNoticia.DataKeys[row.RowIndex].Value.ToString();
                string pathImage = HelperClasses.Utils.GetConfig(HelperClasses.Parameters.NOTICIAS_PATH) + noticiaId + ".jpg";

                if (System.IO.File.Exists(pathImage))
                    imgNoticia.ImageUrl = pathImage;
                else
                    imgNoticia.ImageUrl = "~/Images/Style/sin_foto.jpg";

                //Deshabilita edición noticias si no existen permisos
                ImageButton btnVerMasNoticia = row.FindControl("btnVerMasNoticia") as ImageButton;
                ImageButton btnEditNoticia = row.FindControl("btnEditNoticia") as ImageButton;
                ImageButton btnRemoveNoticia = row.FindControl("btnRemoveNoticia") as ImageButton;

                if (Session[HelperClasses.Parameters.USERID] == null || Session[HelperClasses.Parameters.USERNAME] == null)
                {
                    btnVerMasNoticia.Visible = false;
                    btnEditNoticia.Visible = false;
                    btnRemoveNoticia.Visible = false;
                }

            }
        }

        protected void btnVerMasNoticia_Click(object sender, ImageClickEventArgs e)
        {
            GridViewRow row = ((ImageButton)sender).Parent.Parent as GridViewRow;
            int noticiaId = Int32.Parse(gvNoticia.DataKeys[row.RowIndex].Value.ToString());
            Data.Noticia noticia = Business.Noticia.GetInstance().GetById(noticiaId);
            popupNoticia.SetTitulo(noticia.Titulo + " " + noticia.Fecha.Value.ToShortDateString());
            popupNoticia.SetNoticia(noticia.Descripcion);
            if(System.IO.File.Exists("~/Images/Noticias/" + noticia.NoticiaId.ToString() + ".jpg"))
                popupNoticia.SetImage(noticia.NoticiaId.ToString() + ".jpg");
            else if (System.IO.File.Exists("~/Images/Noticias/" + noticia.NoticiaId.ToString() + ".png"))
                popupNoticia.SetImage("~/Images/Noticias/" + noticia.NoticiaId.ToString() + ".png");
            else
                popupNoticia.SetImage("~/Images/Style/sin_foto.jpg");

            mpeNoticia.Show();
        }

        protected void gvNoticia_RowDataBound(object sender, GridViewRowEventArgs e)
        { }

        protected void gvNoticia_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            GridViewRow row = gvNoticia.Rows[e.RowIndex];
            int noticiaId = Int32.Parse(gvNoticia.DataKeys[row.RowIndex].Value.ToString());
            Data.Noticia noticia = Business.Noticia.GetInstance().GetById(noticiaId);
            noticia.Titulo = ((TextBox)row.FindControl("txtTitulo")).Text;
            noticia.Descripcion = ((TextBox)row.FindControl("txtDescripcion")).Text;
            Business.Noticia.GetInstance().UpdateNoticia(noticia);

            gvNoticia.EditIndex = -1;
            GetNoticias();
        }

        protected void gvNoticia_RowEditing(object sender, GridViewEditEventArgs e)
        {
            gvNoticia.EditIndex = e.NewEditIndex;
            GetNoticias();
        }

        protected void gvNoticia_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
            gvNoticia.EditIndex = -1;
            GetNoticias();
        }

        protected void btnRemoveNoticia_Click(object sender, ImageClickEventArgs e)
        {
            GridViewRow row = ((ImageButton)sender).Parent.Parent as GridViewRow;
            int noticiaId = Int32.Parse(gvNoticia.DataKeys[row.RowIndex].Value.ToString());
            hiddenNoticiaIdEliminar.Value = noticiaId.ToString();

            string titulo = ((Label)row.FindControl("lblTitulo")).Text;
            popupConfirmarAccion.SetTitulo("Eliminar Noticia");
            popupConfirmarAccion.SetMessage("¿Está seguro que desea eliminar la noticia <b>" + titulo + "</b>?");
            mpeConfirmarAccion.Show();
        }

        protected void btnConfirmarEliminacion_Click(object sender, EventArgs e)
        {
            if (Business.Noticia.GetInstance().DeleteNoticia(Int32.Parse(hiddenNoticiaIdEliminar.Value)))
            {
                if (System.IO.File.Exists(Server.MapPath(HelperClasses.Utils.GetConfig(HelperClasses.Parameters.NOTICIAS_PATH) + hiddenNoticiaIdEliminar.Value + ".png")))
                    System.IO.File.Delete(Server.MapPath(HelperClasses.Utils.GetConfig(HelperClasses.Parameters.NOTICIAS_PATH) + hiddenNoticiaIdEliminar.Value + ".png"));
                else if (System.IO.File.Exists(Server.MapPath(HelperClasses.Utils.GetConfig(HelperClasses.Parameters.NOTICIAS_PATH) + hiddenNoticiaIdEliminar.Value + ".jpg")))
                    System.IO.File.Delete(Server.MapPath(HelperClasses.Utils.GetConfig(HelperClasses.Parameters.NOTICIAS_PATH) + hiddenNoticiaIdEliminar.Value + ".jpg"));
                GetNoticias();
            }

            mpeConfirmarAccion.Hide();
        }

        protected void btnSaveNoticia_Click(object sender, EventArgs e)
        {
            Data.Noticia n = new Data.Noticia();
            n.Titulo = popupNewNoticia.GetTitulo();
            n.Descripcion = popupNewNoticia.GetDesc();
            n.Fecha = DateTime.Now;
            n.UsuarioId = Int32.Parse(Session[HelperClasses.Parameters.USERID].ToString());

            Business.Noticia.GetInstance().InsertNoticia(n);
            mpeNewNoticia.Hide();
            GetNoticias();
        }
    }
}